package com.work.keep.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.work.keep.R;
import com.work.keep.adapter.PeopleViewPagerAdapter;
import com.work.keep.base.BaseActivity;
import com.work.keep.datas.PeopleData;
import com.work.keep.fragment.OtherUserDynamicFragment;
import com.work.keep.fragment.OtherUserPhotoFragment;
import com.work.keep.fragment.PeopleRecordFragment;
import com.work.keep.ui.PullToRefreshCoordinatorLayout;
import com.work.keep.view.MyViewPager;
import com.work.keep.volley.GsonRequest;
import com.work.keep.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;


/**
 * people https://api.gotokeep.com/v1.1/people/56999b2d9951948b57b2c53b
 * 需要将网址前半部分传过去
 * 用到的fragment有:OtherUserDynamicFragment OtherUserPhotoFragment
 * Created by dllo on 16/1/19.
 */
public class PeopleActivity extends BaseActivity {
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    private String _id = "56999b2d9951948b57b2c53b";
    private NetworkImageView otherUserAvater;
    private TextView textDynamicCount, textFollowsCount, textFansCount;
    private Button btnFollows;
    private TabLayout otherTabLayout;
    private ViewPager otherViewPager;
    private PeopleViewPagerAdapter peopleViewPagerAdapter;
    private List<Fragment> fragments;
    private PullToRefreshCoordinatorLayout activityPullToRefreshCoor;

    @Override
    protected View getLayoutView() {
        return null;
    }

    @Override
    protected int getLayout() {
//        return R.layout.activity_people;
        return R.layout.activity_people_pull_to_refresh;
    }

    @Override
    protected void initView() {
        activityPullToRefreshCoor = (PullToRefreshCoordinatorLayout) findViewById(R.id.activityPullToRefreshCoor);
        View view = activityPullToRefreshCoor.getV();
        //people avater
        otherUserAvater = (NetworkImageView) view.findViewById(R.id.otherUserAvater);
        //动态数
        textDynamicCount = (TextView) view.findViewById(R.id.textDynamicCount);
        //关注数
        textFollowsCount = (TextView) view.findViewById(R.id.textFollowsCount);
        //粉丝数
        textFansCount = (TextView) view.findViewById(R.id.textFansCount);
        //关注按钮
        btnFollows = (Button) view.findViewById(R.id.btnFollows);
        //tabLayout
        otherTabLayout = (TabLayout) view.findViewById(R.id.otherTabLayout);
        //viewPager
        otherViewPager = (ViewPager) view.findViewById(R.id.otherViewPager);
    }

    @Override
    protected void initData() {
        _id = getIntent().getStringExtra("_id");
        String url = "https://api.gotokeep.com/v1.1/people/" + _id;
        volleySingleton = VolleySingleton.getInstance();
        imageLoader = volleySingleton._getImageLoader();
        fragments = new ArrayList<>();
        OtherUserDynamicFragment dynamicFragment = new OtherUserDynamicFragment();
        OtherUserPhotoFragment photoFragment = new OtherUserPhotoFragment();
        PeopleRecordFragment recordFragment = new PeopleRecordFragment();
        Bundle args = new Bundle();
        args.putString("url", url);
        dynamicFragment.setArguments(args);
        photoFragment.setArguments(args);
        recordFragment.setArguments(args);
        fragments.add(dynamicFragment);
        fragments.add(photoFragment);
        fragments.add(recordFragment);
        peopleViewPagerAdapter = new PeopleViewPagerAdapter(getSupportFragmentManager(), this, fragments);
        otherViewPager.setAdapter(peopleViewPagerAdapter);
        otherTabLayout.setupWithViewPager(otherViewPager);
        for (int i = 0; i < fragments.size(); i++) {
            TabLayout.Tab tab = otherTabLayout.getTabAt(i);
            tab.setCustomView(peopleViewPagerAdapter.getTabView(i));
        }
        GsonRequest<PeopleData> peopleDataGsonRequest = new GsonRequest<PeopleData>(url, PeopleData.class, new Response.Listener<PeopleData>() {
            @Override
            public void onResponse(PeopleData response) {
                EventBus.getDefault().post(response);
                initOtherUserAvater(response.getData().getAvatar());
                //发布动态总数
                textDynamicCount.setText(response.getData().getStatistics().getTotalEntries() + "");
                //关注的人数
                textFollowsCount.setText(response.getData().getFollowings() + "");
                //粉丝数
                textFansCount.setText(response.getData().getFollowers() + "");
                //设置是否已经关注
                if (response.getData().isHasFollowed()) {
                    btnFollows.setText("已关注");
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> peopleHeaders = new HashMap<>();
                peopleHeaders.put("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NjY1MDBlMTYzZGIwYzlmNTdiMTQwYWQiLCJ1c2VybmFtZSI6Iuaeq-WQmyIsImF2YXRhciI6Imh0dHA6Ly93eC5xbG9nby5jbi9tbW9wZW4vc2t3MmhSR3lXRWcwUzV0NzhkMlRVUnFhYjBzcWJ4RXB4S1BLUFpEOXpwbTNJbkIySzRjRnhLVEJsemdDb3VIcGlia3RkRUluQ0NFcGpIS2FHZ1lwRkp6SVRIVU5NYTNpYmYvMCIsImlhdCI6MTQ1MzE3NTAzMCwiZXhwIjoxNDU1NzY3MDMwLCJpc3MiOiJodHRwOi8vd3d3LmdvdG9rZWVwLmNvbS8ifQ.mlN1pgmF9Dt5NPlq_YqoNvXSObvsCgHcYU-9PssqzDI");
                peopleHeaders.put("X-X", "486bba2566a02d3ae31ed4445366a873b89cf910");
                peopleHeaders.put("Host", "api.gotokeep.com");
                return super.getHeaders();
            }
        };
        volleySingleton._addRequest(peopleDataGsonRequest);
    }

    //otherUserAvater的数据加载方法 要在数据请求之后
    private void initOtherUserAvater(String avatar) {
        otherUserAvater.setDefaultImageResId(R.mipmap.ic_launcher);
        otherUserAvater.setErrorImageResId(R.mipmap.ic_launcher);
        otherUserAvater.setImageUrl(avatar, imageLoader);
    }
}
